package com.service;

import com.entity.BusinessUtilityBill;
import com.entity.BusinessUtilityBillRecord;
import com.exception.BusinessException;
import com.repository.BusinessUtilityRecordRepository;
import com.repository.BusinessUtilityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BusinessUtilityBillRecordService {

    @Autowired
    private BusinessUtilityRecordRepository businessUtilityRecordRepository;

    @Autowired
    private BusinessUtilityRepository businessUtilityRepository;

    public BusinessUtilityBillRecord save(BusinessUtilityBillRecord businessUtilityBillRecord) {
        if (businessUtilityBillRecord.getId() != null) {
            return update(businessUtilityBillRecord);
        }
        BusinessUtilityBill businessUtilityBill =
                businessUtilityRepository.findByProjectNameAndBillMonth(businessUtilityBillRecord.getProjectName(),
                        businessUtilityBillRecord.getBillMonth()).orElseThrow(() ->new BusinessException("该水电费条例不存在")) ;
        businessUtilityBillRecord.setUtilityBillId(businessUtilityBill.getId());
        businessUtilityBillRecord.setTotalFeePayment(businessUtilityBillRecord.getElectricityFeePayment().add(businessUtilityBillRecord.getWaterFeePayment()));
        businessUtilityBill.setElectricityFeePayment(businessUtilityBill.getElectricityFeePayment().add(businessUtilityBillRecord.getElectricityFeePayment()));
        businessUtilityBill.setWaterFeePayment(businessUtilityBill.getWaterFeePayment().add(businessUtilityBillRecord.getWaterFeePayment()));
        businessUtilityBill.setTotalFeePayment(businessUtilityBill.getTotalFeePayment().add(businessUtilityBillRecord.getTotalFeePayment()));
        businessUtilityRepository.save(businessUtilityBill);
        return businessUtilityRecordRepository.save(businessUtilityBillRecord);
    }

    public BusinessUtilityBillRecord update(BusinessUtilityBillRecord businessUtilityBillRecord) {
        BusinessUtilityBill bill =
                businessUtilityRepository.findByProjectNameAndBillMonth(businessUtilityBillRecord.getProjectName(),
                        businessUtilityBillRecord.getBillMonth()).orElseThrow(() ->new BusinessException("该水电费条例不存在"));

        BusinessUtilityBillRecord record = businessUtilityRecordRepository.findById(businessUtilityBillRecord.getId()).orElseThrow(() ->new BusinessException("该记录不存在"));
        businessUtilityBillRecord.setTotalFeePayment(businessUtilityBillRecord.getElectricityFeePayment().add(businessUtilityBillRecord.getWaterFeePayment()));
        bill.setElectricityFeePayment(bill.getElectricityFeePayment().subtract(record.getElectricityFeePayment()).add(businessUtilityBillRecord.getElectricityFeePayment()));
        bill.setWaterFeePayment(bill.getWaterFeePayment().subtract(record.getWaterFeePayment()).add(businessUtilityBillRecord.getWaterFeePayment()));
        bill.setTotalFeePayment(bill.getTotalFeePayment().subtract(record.getTotalFeePayment()).add(businessUtilityBillRecord.getTotalFeePayment()));
        businessUtilityRepository.save(bill);
        return businessUtilityRecordRepository.save(businessUtilityBillRecord);
    }

    public void deleteById(Integer id) {
        BusinessUtilityBillRecord record = businessUtilityRecordRepository.findById(id).orElseThrow(() ->new BusinessException("该记录不存在"));
        BusinessUtilityBill bill = businessUtilityRepository.findById(record.getUtilityBillId()).orElseThrow(() ->new BusinessException("该水电费条例不存在"));
        bill.setElectricityFeePayment(bill.getElectricityFeePayment().subtract(record.getElectricityFeePayment()));
        bill.setWaterFeePayment(bill.getWaterFeePayment().subtract(record.getWaterFeePayment()));
        bill.setTotalFeePayment(bill.getTotalFeePayment().subtract(record.getTotalFeePayment()));
        businessUtilityRecordRepository.deleteById(id);
    }

    public BusinessUtilityBillRecord findById(Integer id) {
        return businessUtilityRecordRepository.findById(id).orElse(null);
    }

    public List<BusinessUtilityBillRecord> findAll() {
        return businessUtilityRecordRepository.findAll();
    }

    public Page<BusinessUtilityBillRecord> findAll(Pageable pageable) {
        return businessUtilityRecordRepository.findAll(pageable);
    }
}